﻿using System;
using System.Collections;
using System.Windows.Forms;

namespace ThreadSafeControls {
    partial class ThreadSafeListView {
        public class ColumnHeaderCollection : IList {

            private readonly ThreadSafeListView _listView;
            private object _syncRoot;

            internal ColumnHeaderCollection(ThreadSafeListView listView) {
                if (listView == null)
                    throw new ArgumentNullException("listView");

                _listView = listView;
            }

            public int Count {
                get { return _listView.ReturnAsync<int>(c => c.Columns.Count); }
            }

            public ThreadSafeColumnHeader this[int index] {
                get { return _listView.ReturnAsync<ColumnHeader>(c => c.Columns[index]).AsThreadSafe(_listView); }
            }

            public ThreadSafeColumnHeader this[string key] {
                get { return _listView.ReturnAsync<ColumnHeader>(c => c.Columns[key]).AsThreadSafe(_listView); }
            }

            public ThreadSafeColumnHeader Add(string text) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(text)).AsThreadSafe(_listView);
            }

            public int Add(ColumnHeader value) {
                return _listView.ReturnAsync<int>(c => c.Columns.Add(value));
            }

            public ThreadSafeColumnHeader Add(string text, int width) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(text, width)).AsThreadSafe(_listView);
            }

            public ThreadSafeColumnHeader Add(string key, string text) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(key, text)).AsThreadSafe(_listView);
            }

            public ThreadSafeColumnHeader Add(string text, int width, HorizontalAlignment textAlign) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(text, width, textAlign)).AsThreadSafe(_listView);
            }

            public ThreadSafeColumnHeader Add(string key, string text, int width) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(key, text, width)).AsThreadSafe(_listView);
            }

            public ThreadSafeColumnHeader Add(string key, string text, int width, HorizontalAlignment textAlign, int imageIndex) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(key, text, width, textAlign, imageIndex)).AsThreadSafe(_listView);
            }

            public ThreadSafeColumnHeader Add(string key, string text, int width, HorizontalAlignment textAlign, string imageKey) {
                return _listView.ReturnAsync<ColumnHeader>(c => c.Columns.Add(key, text, width, textAlign, imageKey)).AsThreadSafe(_listView);
            }

            public void Clear() {
                _listView.UpdateAsync(c => c.Columns.Clear());
            }

            public bool Contains(ColumnHeader value) {
                return _listView.ReturnAsync<bool>(c => c.Columns.Contains(value));
            }

            public int IndexOf(ColumnHeader value) {
                return _listView.ReturnAsync<int>(c => c.Columns.IndexOf(value));
            }

            public void Insert(int index, ColumnHeader value) {
                _listView.UpdateAsync(c => c.Columns.Insert(index, value));
            }

            public void Insert(int index, string text) {
                _listView.UpdateAsync(c => c.Columns.Insert(index, text));
            }

            public void Remove(ColumnHeader column) {
                _listView.UpdateAsync(c => c.Columns.Remove(column));
            }

            public void RemoveAt(int index) {
                _listView.UpdateAsync(c => c.Columns.RemoveAt(index));
            }

            public void RemoveByKey(string key) {
                _listView.UpdateAsync(c => c.Columns.RemoveByKey(key));
            }

            // todo: implement remaining overloads of ThreadSafeListView.ColumnHeaderCollection.Insert

            public IEnumerator GetEnumerator() {
                return _listView.ReturnAsync<IEnumerator>(c => c.Columns.GetEnumerator());
            }

            #region Explicit Interface Members

            object IList.this[int index] {
                get { return this[index]; }
                set { _listView.UpdateAsync(c => ((IList)c.Columns)[index] = value); }
            }

            bool IList.IsFixedSize {
                get { return false; }
            }

            bool IList.IsReadOnly {
                get { return false; }
            }

            bool ICollection.IsSynchronized {
                get { return false; }
            }

            object ICollection.SyncRoot {
                get { return _syncRoot ?? (_syncRoot = new object()); }
            }

            int IList.Add(object value) {
                return _listView.ReturnAsync(c => ((IList)c.Columns).Add(value));
            }

            bool IList.Contains(object value) {
                return _listView.ReturnAsync(c => ((IList)c.Columns).Contains(value));
            }

            void ICollection.CopyTo(Array dest, int index) {
                _listView.UpdateAsync(c => ((ICollection)c.Columns).CopyTo(dest, index));
            }

            int IList.IndexOf(object value) {
                return _listView.ReturnAsync(c => ((IList)c.Columns).IndexOf(value));
            }

            void IList.Insert(int index, object value) {
                _listView.UpdateAsync(c => ((IList)c.Columns).Insert(index, value));
            }

            void IList.Remove(object value) {
                _listView.UpdateAsync(c => ((IList)c.Columns).Remove(value));
            }

            #endregion

        }
    }
}
